package com.example.cn.stock.service;

import com.example.cn.stock.entity.Stock;
import com.example.cn.stock.mapper.StockMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StockService {

    @Autowired
    private StockMapper stockMapper;
    @Transactional
    @GlobalTransactional
    public void reduceStock(Integer productId) {
//        Stock stock = stockMapper.selectById(productId);
//        if (stock == null || stock.getCount() < count) {
//            return "fail";
//        }
//        stock.setCount(stock.getCount() - count);
//        stockMapper.updateById(stock);
//        return "success";

        Stock stock = stockMapper.selectById(productId);
        stock.setCount(stock.getCount()-1);
        stockMapper.updateById(stock);
    }
}
